package com.cz.pg;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.util.*;

public class GPInsert {
    // Common letters for stock tickers
    private static final char[] COMMON_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
    // More frequent characters that appear in tickers
    private static final char[] FREQUENT_LETTERS = "ABCDEFGIMNPRSTX".toCharArray();
    // Common industries for prefixes
    private static final String[] INDUSTRY_PREFIXES = {"BIO", "MED", "TECH", "FIN", "OIL", "CYBER",
            "DATA", "AI", "CLOUD", "NET", "NANO", "SOLAR",
            "AUTO", "ROBO", "AGRI", "FOOD", "PHARMA", "GREEN"};
    public static void main(String[] args) {
        List<String> tickers = generateUniqueTickers(15000000);
        List<Entity> entities = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < 15000000; i++) {
            Entity entity = new Entity("stocks_real_time");

            // 随机选择股票代码
            String symbol = tickers.get(i);

            // 生成 10 - 2000 之间的价格（保留两位小数）
            double price = 10 + (2000 - 10) * random.nextDouble();
            price = Double.parseDouble(String.format("%.2f", price));
            // 生成 1000 - 10,000,000 之间的交易量
            int dayVolume = random.nextInt(10000000 - 1000) + 1000;

            // 设置数据
            //entity.set("time", new Date());
             random = new Random();

// 生成 2018 到 2025 之间的随机日期
            Calendar calendar = Calendar.getInstance();
            calendar.set(2018, Calendar.JANUARY, 1, 0, 0, 0); // 设置起始日期为2018年1月1日
            long startMillis = calendar.getTimeInMillis();

            calendar.set(2025, Calendar.DECEMBER, 31, 23, 59, 59); // 设置结束日期为2025年12月31日
            long endMillis = calendar.getTimeInMillis();

            long randomMillisSinceEpoch = startMillis + (long) (random.nextDouble() * (endMillis - startMillis));
            Date randomDate = new Date(randomMillisSinceEpoch);
            entity.set("time", randomDate);
            entity.set("symbol", symbol);
            entity.set("price", price);
            entity.set("day_volume", dayVolume);

            // 添加到列表
            entities.add(entity);
            if(entities.size() % 500 == 0){
                try {
                    Db.use("pg").insert(entities);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                entities = new ArrayList<Entity>();
                System.out.println("插入数据:500条");
            }
        }
    }

    public static List<String> generateUniqueTickers(int count) {
        List<String> tickers = new ArrayList<>();
        Random random = new Random();

        // Add some example real tickers first
        String[] realTickers = {"AAPL", "GOOGL", "AMZN", "TSLA", "MSFT", "NFLX", "FB", "IBM",
                "NVDA", "AMD", "INTC", "ORCL", "CRM", "JPM", "BAC", "GS", "WMT",
                "TGT", "KO", "PEP", "MCD", "SBUX", "DIS", "NFLX", "PYPL", "V",
                "MA", "UNH", "JNJ", "PFE", "MRNA", "CVX", "XOM"};

        for (String ticker : realTickers) {
            tickers.add(ticker);
        }

        // Generate the rest of unique tickers
        while (tickers.size() < count) {
            String ticker = "";

            int type = random.nextInt(5);
            switch (type) {
                case 0: // 2-letter ticker
                    ticker = generateRandomLetters(2, random);
                    break;
                case 1: // 3-letter ticker
                    ticker = generateRandomLetters(3, random);
                    break;
                case 2: // 4-letter ticker
                    ticker = generateRandomLetters(4, random);
                    break;
                case 3: // 5-letter ticker
                    ticker = generateRandomLetters(5, random);
                    break;
                case 4: // Industry prefix + 1-2 letters
                    String prefix = INDUSTRY_PREFIXES[random.nextInt(INDUSTRY_PREFIXES.length)];
                    int suffixLength = random.nextInt(2) + 1;
                    ticker = prefix + generateRandomLetters(suffixLength, random);
                    break;
            }

            // Occasionally add a suffix like .X to represent non-US exchanges
            if (random.nextInt(20) == 0) {
                char suffix = (char)('A' + random.nextInt(26));
                ticker = ticker + "." + suffix;
            }

            tickers.add(ticker);
        }

        return tickers;
    }

    private static String generateRandomLetters(int length, Random random) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            // For the first letter, more likely to use frequent letters
            char[] sourceArray = (i == 0 || random.nextInt(3) == 0) ?
                    FREQUENT_LETTERS : COMMON_LETTERS;
            sb.append(sourceArray[random.nextInt(sourceArray.length)]);
        }
        return sb.toString();
    }
}
